package com.gzh.system.service.impl;

import com.gzh.core.chche.dao.mapper.TshoolMapper;
import com.gzh.core.chche.dao.po.TschoolEntityCache;
import com.gzh.core.chche.service.CacheManage;
import com.gzh.core.chche.service.dto.School;
import com.gzh.core.dao.mapper.AcademyMapper;
import com.gzh.core.dao.mapper.ClassMapper;
import com.gzh.core.dao.mapper.MenuMapper;
import com.gzh.core.dao.mapper.UserMapper;
import com.gzh.core.dao.po.AcademyEntity;
import com.gzh.core.dao.po.ClassEntity;
import com.gzh.core.dao.po.MenuEntity;
import com.gzh.core.dao.po.UserEntity;
import com.gzh.core.util.StringUtils;
import com.gzh.system.service.CommonService;
import com.gzh.system.service.dto.ALLClassInfo;
import com.gzh.system.service.dto.ALLClassOut;
import com.gzh.system.service.dto.SchoolQueryIn;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import tk.mybatis.mapper.entity.Condition;
import tk.mybatis.mapper.entity.Example;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @Author guanzh
 * @Date 2022/3/2 14:41
 * @Description
 */
@Service
@Slf4j
public class CommonServiceImpl implements CommonService {

    @Autowired
    private TshoolMapper tshoolMapper;
    @Autowired
    private ClassMapper classMapper;
    @Autowired
    private AcademyMapper academyMapper;
    @Autowired
    private CacheManage cacheManage;
    @Autowired
    private MenuMapper menuMapper;
    @Autowired
    private UserMapper userMapper;

    @Override
    public List<School> getSchoolList(SchoolQueryIn in) {
        String findStr = in.getFindStr();
        findStr = "%" + findStr + "%";
        Condition condition = new Condition(TschoolEntityCache.class);
        Example.Criteria criteria = condition.createCriteria();
        criteria.orLike("schoolId",findStr);
        criteria.orLike("schoolName",findStr);
        criteria.orLike("schoolCode",findStr);
        List<TschoolEntityCache> allList = tshoolMapper.selectByCondition(condition);
        //List<School> allList = cacheManage.getSchoolCache().getObjectListByKeyword(findStr);
        if (allList.size() > 100){
            allList = allList.subList(0,100);
        }
        List<School> resultList = allList.stream().map(t -> new School(t)).collect(Collectors.toList());
        return resultList;
    }

    @Override
    public List<ALLClassOut> getALLClass() {
        Condition classCondition = new Condition(ClassEntity.class);
        classCondition.createCriteria().andEqualTo("status",1);
        Condition acadmyCondition = new Condition(AcademyEntity.class);
        acadmyCondition.createCriteria().andEqualTo("status",1);
        List<ALLClassOut> allClassOuts = new ArrayList<>();
        List<ClassEntity> classEntities = classMapper.selectByCondition(classCondition);
        List<AcademyEntity> academyEntities = academyMapper.selectByCondition(acadmyCondition);
        for (AcademyEntity academyEntity : academyEntities) {
            ALLClassOut allClassOut = new ALLClassOut();
            List<ALLClassInfo> allClassInfos = new ArrayList<>();
            allClassOut.setValue(StringUtils.toString(academyEntity.getAcademyId()));
            allClassOut.setLabel(academyEntity.getAcademyName());
            List<ClassEntity> entityList = classEntities.stream().filter(t -> t.getTacademyId().equals(academyEntity.getAcademyId())).collect(Collectors.toList());
            for (ClassEntity classEntity : entityList) {
                ALLClassInfo info = new ALLClassInfo();
                info.setLabel(classEntity.getClassName());
                info.setValue(StringUtils.toString(classEntity.getClassId()));
                allClassInfos.add(info);
            }
            allClassOut.setChildren(allClassInfos);
            allClassOuts.add(allClassOut);
        }
        return allClassOuts;
    }

    @Override
    public List<MenuEntity> queryMenu(Integer userId) {
        UserEntity userQuery = new UserEntity();
        userQuery.setUserId(userId);
        UserEntity user = userMapper.selectOne(userQuery);
        Integer roleId = user.getRoleId();
        Condition menuCondition = new Condition(MenuEntity.class);
        menuCondition.createCriteria().andEqualTo("roleId",roleId);
        List<MenuEntity> menuEntityList = menuMapper.selectByCondition(menuCondition);
        return menuEntityList;
    }
}
